
import numpy as np

# 将列表转换为ndarray
def fun1():
    x = [1, 2, 3]
    a = np.asarray(x)
    print(a)

# 将元组转换为ndarray
def fun2():
    x = (1, 2, 3)
    a = np.asarray(x)
    print(a)

# 将元组列表转换为ndarray
def fun3():
    x = [(1, 2, 3), (4, 5, 6)]
    a = np.asarray(x)
    print(a)

# 设置了dtype参数
def fun4():
    x = [1, 2, 3]
    a = np.asarray(x, dtype=float)
    print(a)

# numpy.frombuffer 用于实现动态数组
def fun5():
    s = b'Hello World'
    a = np.frombuffer(s, dtype='S1')
    print(a)

# numpy.formiter 用于从可迭代对象中建立ndarray对象，返回一维数组
def fun6():
    # 使用range函数创建列表对象
    list = range(5)
    it = iter(list)

    # 使用迭代器创建ndarray
    x = np.fromiter(it, dtype=float)
    print(x)

if __name__ == '__main__':
    fun6()